﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace UmcProvider
{
    public sealed class CodeDomLoader
    {

        public XmlDocument Serialize(IDesignerHost host)
        {
            XmlDocument xml = new XmlDocument();

            char quotationMark = '"';

            //use <msc> instead of <Iuc>
            //string initializeStr = string.Format("<Iuc><Head><Info Name={0}Code{0}>1</Info><Info Name={0}Reason{0}>成功返回</Info><Info Name={0}Session{0}></Info></Head></Iuc>", quotationMark);
            string initializeStr = string.Format("<msc><Head><Info Name={0}Code{0}>1</Info><Info Name={0}Reason{0}>成功返回</Info><Info Name={0}Session{0}></Info></Head></msc>", quotationMark);
            xml.LoadXml(initializeStr);

            Control umcBody = (host.RootComponent as UmcHost).Body;

            XmlNode body = xml.DocumentElement.AppendChild(
                UmcSerializeFactory.Create(umcBody).Serialize(umcBody, xml).First());

            foreach (IComponent child in (umcBody).Controls)
            {
                IConverter ict = UmcSerializeFactory.Create(child);
                if (ict != null)
                {
                    XmlNode[] nodes = ict.Serialize(child, xml);

                    foreach (var node in nodes)
                        body.AppendChild(node);
                }
            }

            //xml.Save("Page1.umc");
            //Process.Start("notepad", "Page1.umc");
            return xml;
        }




        public void DeSerialize(IDesignerHost host, XmlDocument xml)
        {
            Control umcBody = (host.RootComponent as UmcHost).Body;

            List<IComponent> oldList = new List<IComponent>();
            List<IComponent> newList = new List<IComponent>();

            foreach (IComponent obj in host.Container.Components.Cast<IComponent>().Skip(1))
            {
                if (!(obj is Body))
                {
                    oldList.Add(obj);
                    host.Container.Remove(obj);
                }
            }

            try
            {
                foreach (XmlNode node in xml.SelectSingleNode("//Body").ChildNodes)
                {
                    IConverter ict = UmcDeSerializeFactory.Create(node.Name);
                    if (ict != null)
                    {
                        //if (ict is UmcMenuStrip)
                        //{
                        //    IComponent component = ict.DeSerialize(host, xml, xml.SelectNodes("/Iuc/Body/MenuItem").Cast<XmlNode>().ToArray());
                        //    newList.Add(component);
                        //}
                        //else
                        {
                            IComponent component = ict.DeSerialize(host, xml, new XmlNode[] { node });
                            newList.Add(component);
                        }
                    }
                }
            }
            catch
            {
                //TODO
                foreach (IComponent obj in host.Container.Components.Cast<IComponent>().Skip(1))
                {
                    if (!(obj is Body))
                        host.Container.Remove(obj);
                }
                foreach (IComponent obj in oldList)
                {
                    if (obj is Control)
                        umcBody.Controls.Add((Control)obj);
                    else
                        host.Container.Add(obj);
                }
            }

            foreach (IComponent obj in newList)
            {
                if (obj is Control)
                    umcBody.Controls.Add((Control)obj);
                else
                    host.Container.Add(obj);
            }
        }


    }


}
